package 算法;

public class 课程表 {

    public boolean canFinish(int numCourses, int[][] prerequisites) {
        int[] res = new int[numCourses];
        for (int[] prerequisite : prerequisites) {
            res[prerequisite[0]] = res[prerequisite[0]]+1;
        }

        boolean flg = true;
        while (flg){
            flg = false;
            for (int i = 0; i < res.length; i++) {
                if(res[i] == 0){
                    for (int[] prerequisite : prerequisites) {
                        if(prerequisite[1] == i){
                            res[prerequisite[0]] = res[prerequisite[0]]-1;
                        }
                    }
                    res[i]=-1;
                    flg = true;
                    break;
                }
            }
        }

        for (int re : res) {
            if(re != -1) return false;
        }
        return true;
    }

}
